package com.chain.qs.modules.biz.service;

import com.chain.qs.common.service.CrudService;
import com.chain.qs.modules.biz.dao.StationDao;
import com.chain.qs.modules.biz.entity.EquipmentGroup;
import com.chain.qs.modules.biz.entity.Station;
import com.chain.qs.modules.biz.entity.StationGroup;
import com.chain.qs.modules.sys.entity.Office;
import com.chain.qs.modules.sys.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 发射站Service
 *
 * @author wangzhiping
 */
@Service
//@Transactional(readOnly = true)
public class StationService extends CrudService<StationDao, Station> {

    @Resource
    private StationGroupService stationGroupService;

    public List<Station> findListWithOffice(Office station) {
        return this.dao.findListWithOffice(station);
    }

    @Override
    public Station get(String id) {
        Station station = super.get(id);
        if (station != null) {
            List<EquipmentGroup> equipmentGroups = dao.findEquGroup(station);
            station.setEquipmentGroups(equipmentGroups);
        }
        return station;
    }

    @Override
    @Transactional(readOnly = false)
    public void save(Station entity) {
        super.save(entity);
//        dao.deleteEquGroup(entity);
//
//        if(entity.getEquipmentGroups()!=null && entity.getEquipmentGroups().size()>0)
//        dao.insertEquGroup(entity);
    }

    public ArrayList<Map<String, String>> getStationById(String stationId) {
        return dao.getStationById(stationId);
    }

    @Transactional(readOnly = false)
    public void delete(Station station) {
        for (StationGroup stationGroup : stationGroupService.findListByOfficeId(station.getId())) {
            stationGroupService.delete(stationGroup);
        }
        super.delete(station);
        UserUtils.removeCache(UserUtils.CACHE_STATION_LIST);
    }

}
